package org.example.Controller;


import org.example.Service.UserService;
import org.example.domain.Address;
import org.example.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired

    private UserService userService;

    @RequestMapping(value = "/users",method = RequestMethod.POST)
    @ResponseBody
    public String save(User user) {
        System.out.println("save");
        return userService.save(user);
    }
    @RequestMapping(value = "/users/{id}",method = RequestMethod.DELETE)
    @ResponseBody
    public String delete(@PathVariable Integer id) {
        System.out.println("delete");
        return userService.delete(id);
    }


    @RequestMapping("/commonParams")
    @ResponseBody
    public String commonParams(String name, Integer age) {
        System.out.println("普通参数传递" + name + "--" + age);
        return userService.commonParams(name, age);
    }

    @RequestMapping("/commonDifferentParams")
    @ResponseBody
    public String commonDifferentParams(@RequestParam("name") String userName, Integer age) {
        System.out.println("普通参数传递" + userName + "--" + age);
        return userService.commonParams(userName, age);
    }

    @RequestMapping("/pojoParams")
    @ResponseBody
    public String pojoParams(User user) {
        System.out.println(user);
//        System.out.println("Address: " + (user.getAddress() != null ? user.getAddress().getCity() : "null"));
        System.out.println(user.getAddress().getCity()+"====="+user.getAddress().getCity());
        return userService.pojoParams(user);
    }
    @RequestMapping("/arrayParams")
    @ResponseBody
    public String listParams(String[] likes ) {
        System.out.println(Arrays.toString(likes));
        return "所有爱好";
    }
    @RequestMapping("/listParams")
    @ResponseBody
    public String listParams(@RequestParam List<String> likes ) {
        System.out.println(likes);
        return "所有爱好";
    }
    @RequestMapping("/listForJsonParams")
    @ResponseBody
    public String listForJsonParams(@RequestBody List<String> likes ) {
        System.out.println(likes);
        return "所有爱好";
    }

    @RequestMapping("/pojoForJsonParams")
    @ResponseBody
    public String userForJsonParams(@RequestBody User user ) {
        System.out.println(user);
        return "所有爱好";
    }
    @RequestMapping("/listPojoForJsonParams")
    @ResponseBody
    public String listPojoForJsonParams(@RequestBody List<User> users ) {
        System.out.println(users);
        return "所有爱好";
    }
    @RequestMapping("/dateParams")
    @ResponseBody
    public String dateParams(Date date,
                             @DateTimeFormat(pattern = "yyyy-MM-dd") Date date1,
                             @DateTimeFormat(pattern ="yyyy/MM/dd HH:mm:ss") Date date2) {
        System.out.println(date);
        System.out.println(date1);
        System.out.println(date2);
        return "所有爱好";
    }

    @RequestMapping("/toJumpPage")
    public String toJumpPage() {
        System.out.println("跳转页面");
        return "jumpPage";
    }
    @RequestMapping("/toJsonPojo")
    @ResponseBody
    public User toJsonPojo() {
        System.out.println("返回json数据");
        User user = new User();
        user.setName("张三");
        user.setAge(18);
        return user;
    }
    @RequestMapping("/toJsonList")
    @ResponseBody
    public List<User> toJsonList() {
        System.out.println("返回json集合数据");
        User user = new User();
        user.setName("张三");
        user.setAge(18);
        user.setAddress(new Address());
        User user1 = new User();
        user1.setName("李四");
        user1.setAge(44);
        user1.setAddress(new Address());
        List<User> list = new ArrayList<>();
        list.add(user);
        list.add(user1);
        return list;
    }
    }

